package com.xg.java.controller;

import com.xg.java.entity.SysUser;
import com.xg.java.entity.User;
import com.xg.java.resp.Response;
import com.xg.java.service.SysUserService;
import io.swagger.annotations.*;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * @description sys_user
 * @author wang
 * @date 2021-11-23
 */
@Slf4j
@RestController
@Api(tags = "登录管理")
@RequestMapping("/login")
public class LoginUserController {


    @Resource
    private Response response;

    @Resource
    private SysUserService sysUserService;

    /**
     * create by: YHY
     * description: 登录
     * create time: 2020/6/28 17:11
     *
     * @return 登录结果
     */
    @CrossOrigin
    @ResponseBody
    @ApiOperation(value = "POST",notes = "登录结果")
    @ApiResponses({
            @ApiResponse(code = 200,message = "OK",response = SysUser.class),
    })
    @RequestMapping(value = "/loginUser", produces = MediaType.APPLICATION_JSON_VALUE,method = RequestMethod.POST)
    public Response login(@ApiParam(value = "User对象",name="User") User user) {
        log.info("进入登录.....");
        String userName = user.getUserName();
        String password = user.getPwd();
        if (StringUtils.isBlank(userName)) {
            return response.failure(1001, "用户名为空！");
        }

        if (StringUtils.isBlank(password)) {
            return response.failure(1002, "密码为空！");
        }

        SysUser sysUser = this.sysUserService.loginUser(userName,password);
        //loginUser.setToken(SecurityUtils.getSubject().getSession().getId().toString());
        //获取当前用户的审核状态
        //UserInfo userInfo = userInfoService.getUserInfoState(loginUser.getUserId());
        //loginUser.setRecognizeState(userInfo.getState());
        //记录用户登录时间
        if (sysUser == null ) {
            // 登录成功返回用户信息
            return this.response.failure(1265,"抱歉用户，用户名或密码错误");
        }else {
            return response.success(200, "登录成功！", sysUser);
        }
    }
}
